//
//  SecondViewController.m
//  UI_豆瓣更新版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "SecondViewController.h"
#import "MovieTableViewCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "MovieCollectionViewCell.h"
#import "DetailMovieViewController.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arrData;

@end

@implementation SecondViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"电影";
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [item release];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -  64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self dataHandle];
    
}

// 右item点击方法
- (void)itemAction:(UIBarButtonItem *)item {
    
    
    UIImage *img = [UIImage imageNamed:@"btn_nav_list@2x.png"];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(itemAction1:)];
    self.navigationItem.rightBarButtonItem = item1;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的大小
    flowLayout.itemSize = CGSizeMake(111, 150);
    // 设置每个item的最小列间距(默认是10)
    flowLayout.minimumInteritemSpacing = 10;
    // 设置每个item的最大行间距(默认是10)
    flowLayout.minimumLineSpacing = 50;
    // 设置分区间隔(上,下,左,右)
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 设置 UICollectionView 的滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 尾部引用的尺寸
    flowLayout.footerReferenceSize = CGSizeMake(100, 150);
    
    // 创建一个UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    [collectionView registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
    [self.view addSubview:collectionView];
    [flowLayout release];
    [collectionView release];
}



#pragma mark - UICollectionView Delegate
// 返回item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrData.count;
}
// 创建item的试图对象
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    MovieModel *model = _arrData[indexPath.row];
    cell.nameLabel.text = model.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];
    return cell;
}
// 右边 按钮 点击方法
- (void)itemAction1:(UIBarButtonItem *)item {
    UIBarButtonItem *iTem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = iTem;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [iTem release];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DetailMovieViewController *dmVc = [[DetailMovieViewController alloc] init];
    [self.navigationController pushViewController:dmVc animated:YES];
}



#pragma mark - UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MovieModel *model = _arrData[indexPath.row];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:model.image]];
    cell.nameLabel.text = model.title;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}
// tableView 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailMovieViewController *dmVc = [[DetailMovieViewController alloc] init];
    [self.navigationController pushViewController:dmVc animated:YES];
}


- (void)dataHandle {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSMutableArray *tempArr = [NSMutableArray arrayWithContentsOfFile:path];
    self.arrData = [NSMutableArray array];
    for (NSDictionary *dic in tempArr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_arrData addObject:model];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
